package com.bdqn.base.jedis;

import com.bdqn.base.Global;
import redis.clients.jedis.Jedis;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Redis操作工具
 * 
 * @author Lilibo
 *
 */
public class RedisTool {

	private static RedisClient redisClient = RedisClient.getInstance();

	/**
	 * Keys 获取所有匹配的Key
	 * 
	 * @param pattern
	 */
	public static Set<String> keys(String pattern) {
		Jedis jedis = redisClient.jedis();
		boolean isbroken = false;
		try {
			return jedis.keys(pattern);
		} catch (Exception e) {
			isbroken = true;
			e.printStackTrace();
			return null;
		} finally {
			redisClient.release(jedis, isbroken);
		}
	}

	/**
	 * Keys 获取键值
	 * 
	 * @param key
	 * @return
	 */
	public static String get(String key) {
		Jedis jedis = redisClient.jedis();
		boolean isbroken = false;
		try {
			return jedis.get(key);
		} catch (Exception e) {
			isbroken = true;
			e.printStackTrace();
			return null;
		} finally {
			redisClient.release(jedis, isbroken);
		}
	}

	/**
	 * Keys 设置键值对
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public static String set(String key, String value) {
		Jedis jedis = redisClient.jedis();
		boolean isbroken = false;
		try {
			return jedis.set(key, value);
		} catch (Exception e) {
			isbroken = true;
			e.printStackTrace();
			return null;
		} finally {
			redisClient.release(jedis, isbroken);
		}
	}

	/**
	 * 删除某个key
	 * @param key
	 * @return
	 */
	public static long del(String key) {
		Jedis jedis = redisClient.jedis();
		boolean isbroken = false;
		try {
			return  jedis.del(key);
		} catch (Exception e) {
			isbroken = true;
			e.printStackTrace();
			return 0;
		} finally {
			redisClient.release(jedis, isbroken);
		}
	}
	
	
	/**
	 * 自增
	 * @param key
	 * @return
	 */
	public static long incr(String key){
		Jedis jedis = redisClient.jedis();
		boolean isbroken = false;
		try {
			return  jedis.incr(key);
		} catch (Exception e) {
			isbroken = true;
			e.printStackTrace();
			return 0;
		} finally {
			redisClient.release(jedis, isbroken);
		}
	}
	 

	/**
	 * list	 类型 删除 指定元素	 
	 * @param key
	 * @param count
	 * @return
	 */
	public static long lrem(String key,long count,String values){
		Jedis jedis = redisClient.jedis();
		boolean isbroken = false;
		try {
			return  jedis.lrem(key, count, values);
		} catch (Exception e) {
			isbroken = true;
			e.printStackTrace();
			return 0;
		} finally {
			redisClient.release(jedis, isbroken);
		}
	}
	
	/**
	 * list类型的取值
	 * @param key ,start,end
	 * @return
	 */
	public static  List<String> lrange(String key,long start,long end){
		Jedis jedis = redisClient.jedis();
		boolean isbroken = false;
		try {
			return  jedis.lrange(key, start, end);
		} catch (Exception e) {
			isbroken = true;
			e.printStackTrace();
			return null;
		} finally {
			redisClient.release(jedis, isbroken);
		}
	}
	
	/**
	 * list类型向列表左边添加记录
	 * @param key ,values
	 * @return
	 */
	public static long lpush(String key,String values){
		Jedis jedis = redisClient.jedis();
		boolean isbroken = false;
		try {
			return  jedis.lpush(key, values);
		} catch (Exception e) {
			isbroken = true;
			e.printStackTrace();
			return 0;
		} finally {
			redisClient.release(jedis, isbroken);
		}
	}

	/**
	 * Hash 设置Map<String, String>
	 * 
	 * @param key
	 * @param map
	 */
	public static void hsetMap(String key, Map<String, String> map) {
		Jedis jedis = redisClient.jedis();
		boolean isbroken = false;
		try {
			for (String field : map.keySet()) {
				jedis.hset(key, field, map.get(field));
			}
		} catch (Exception e) {
			isbroken = true;
			e.printStackTrace();
		} finally {
			redisClient.release(jedis, isbroken);
		}
	}

	/**
	 * Hash 获取Map<String, String>
	 * 
	 * @param key
	 * @return
	 */
	public static Map<String, String> hgetMap(String key) {
		Jedis jedis = redisClient.jedis();
		boolean isbroken = false;
		try {
			return jedis.hgetAll(key);
		} catch (Exception e) {
			isbroken = true;
			e.printStackTrace();
			return null;
		} finally {
			redisClient.release(jedis, isbroken);
		}
	}

	/**
	 * Keys 根据UID获取Session的Keys
	 * 
	 * @param uid
	 */
	public static Set<String> keysSession(String uid) {
		Jedis jedis = redisClient.jedis();
		boolean isbroken = false;
		try {
			String pattern = Global.SESSION + Global.CACHE_POINT + uid + "*";
			return jedis.keys(pattern);
		} catch (Exception e) {
			isbroken = true;
			e.printStackTrace();
			return null;
		} finally {
			redisClient.release(jedis, isbroken);
		}
	}


	/*public static void main(String[] args) {
		// redisClient.jedis().flushDB();
		
		String pattern = "SESSION:*"; testKeys(pattern);
		
		String key = "SESSION:79941097658C437EAFC5A391A6609870@footgps.com/android:261402284868853" ; testHsetMap(key); testHgetMap(key);
		
		String jid = "79941097658C437EAFC5A391A6609870@footgps.com/android:261402284868853" ; testHsetSession(jid); testHgetSession(jid);
		
		String uid = "169FB59FF40F48BEBE1456327F3329B8*"; testKeysSession(uid);
		 
	}*/

	public static void testKeys(String pattern) {
		Set<String> keys = RedisTool.keys(pattern);
		Iterator<String> iterator = keys.iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			System.out.println(key);
		}
	}

}
